<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>我的在线OJ平台</title>
    <meta name="description" content="Charcoal is a free Bootstrap 4 UI kit build by @attacomsian at Wired Dots." />
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <!--Bootstrap 4-->
    <link rel="stylesheet" href="css/bootstrap.min.css">
</head>
<body>

<nav class="navbar navbar-expand-md navbar-dark fixed-top sticky-navigation">
    <a class="navbar-brand font-weight-bold" href="#">在线OJ</a>
    <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#topMenu" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
        <span class="navbar-toggler-icon"></span>
    </button>

    <div class="collapse navbar-collapse" id="topMenu">
    </div>
</nav>

<!--hero section-->
<section class="bg-hero">
    <div class="container">
        <div class="row vh-100">
            <div class="col-sm-12 my-auto text-center">
                <h1>在线 OJ</h1>
                <p class="lead text-capitalize my-4">
                    基于 JAVA ServLet 搭建的在线 OJ 平台
                </p>
                <a href="https://gitee.com/ke-qing-saigao/java3/tree/master/java_oj" class="btn btn-outline-light btn-radius btn-lg">搭建思路</a>
            </div>
        </div>
    </div>
</section>

<!--components-->
<section class="my-5 pt-5">
    <div class="container">
        <div class="row mt-4">
            <div class="col-sm-12 pb-4">
                <div class="jumbotron jumbotron-fluid">
                    <div class="container" id="problemDesc">
                        <!-- <h1>Container fluid size jumbotron</h1>
                        <p>Think BIG with a Bootstrap Jumbotron!</p> -->
                    </div>
                </div>
            </div>
        </div>

        <form id="updateProblem">
<!--            <input type="hidden" class="form-control" id="problemId" name="id">-->
            <div class="form-group">
                <label for="title">题目名称:</label>
                <input type="text" class="form-control" id="title" name="title">
            </div>
            <div class="form-group">
                <label for="desc">题目描述</label>
                <input type="text" class="form-control" id="desc" name="description" />
            </div>
            <div class="form-group">
                <label for="level">题目难度</label>
                <input type="text" class="form-control" id="level" name="level" />
            </div>

            <div class="form-group">
                <label for="level">题目答案</label>
                <input type="text" class="form-control" id="answer" name="level" />
            </div>

        </form>

        <div class="row mt-4">
            <div class="col-sm-12 pb-4">
                <div class="form-group">
                    <label for="codeEditor1">样例代码编辑框</label>
                    <div id="editor1" style="min-height:400px">
                        <textarea class="form-control" id="codeEditor1" style="width: 100%; height: 400px;"></textarea>
                    </div>
                </div>
            </div>
        </div>

        <div class="row mt-4">
            <div class="col-sm-12 pb-4">
                <div class="form-group">
                    <label for="codeEditor2">测试代码编辑框</label>
                    <div id="editor2" style="min-height:400px">
                        <textarea class="form-control" id="codeEditor2" style="width: 100%; height: 400px;"></textarea>
                    </div>
                </div>
            </div>
        </div>

        <button type="button" id="submitButton">提交</button>

        <div class="row mt-4">
            <div class="col-sm-12 pb-4">
                <div class="jumbotron jumbotron-fluid">
                    <div class="container">
                                <pre id="problemResult">

                                </pre>
                        <!-- <h1>Container fluid size jumbotron</h1>
                        <p>Think BIG with a Bootstrap Jumbotron!</p> -->
                    </div>
                </div>
            </div>
        </div>

    </div>

</section>

<!--footer-->
<section class="py-5 bg-dark">
    <div class="container">
        <div class="row">
            <div class="col-md-6 offset-md-3 col-sm-8 offset-sm-2 col-xs-12 text-center">
                <!-- <h3>Upgrade to Pro Version</h3>
                <p class="pt-2">
                    We are working on <b>Charcoal Pro</b> which will be released soon. The pro version
                    will have a lot more components, sections, icons, plugins and example pages.
                    Join the waiting list to get notified when we release it (plus discount code).
                </p>
                <a class="btn btn-warning" href="https://wireddots.com/newsletter">Join Waiting List</a>
                <hr class="my-5"/> -->
                <p class="pt-2 text-muted">
                    &copy; 刻晴赛高
                </p>
            </div>
        </div>
    </div>
</section>

<script src="https://code.jquery.com/jquery-3.1.1.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.3/umd/popper.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta.2/js/bootstrap.min.js"></script>
<script src="js/app.js"></script>
<!-- 引入 ace.js -->
<script src="https://cdn.bootcdn.net/ajax/libs/ace/1.2.9/ace.js"></script>
<script src="https://cdn.bootcdn.net/ajax/libs/ace/1.2.9/ext-language_tools.js"></script>
<script>

    let problemDesc = document.querySelector("#problemDesc");
    let h3 = document.createElement("h3");
    h3.innerHTML = "添加题目"
    problemDesc.appendChild(h3);

    function initAce() {
        // 参数 editor 就对应到刚才在 html 里加的那个 div 的 id
        let editor = ace.edit("editor1");
        editor.setOptions({
            enableBasicAutocompletion: true,
            enableSnippets: true,
            enableLiveAutocompletion: true
        });
        editor.setTheme("ace/theme/twilight");
        editor.session.setMode("ace/mode/java");
        editor.resize();
        document.getElementById('editor1').style.fontSize = '20px';

        return editor;
    }

    function initAce2() {
        // 参数 editor 就对应到刚才在 html 里加的那个 div 的 id
        let editor = ace.edit("editor2");
        editor.setOptions({
            enableBasicAutocompletion: true,
            enableSnippets: true,
            enableLiveAutocompletion: true
        });
        editor.setTheme("ace/theme/twilight");
        editor.session.setMode("ace/mode/java");
        editor.resize();
        document.getElementById('editor2').style.fontSize = '20px';

        return editor;
    }

    let editor2 = initAce2();//测试代码编辑框
    let editor1 = initAce();//样例代码编辑框

    function getProblem(){
        $.ajax({
            url: "problem/TemAndTestCode",
            type: "GET",
            success: function(data){
                let problem = data.data;
                editor1.setValue(problem.templateCode);
            }
        })
    }
    getProblem();

    let submitButton = document.querySelector("#submitButton");
    submitButton.onclick = function() {
        let title = $("#title").val();
        let level = $("#level").val();
        let desc = $("#desc").val();
        let answer = $("#answer").val();
        if((title == null || title == "") || (level == null || level == "") || (desc == null || desc == "") || (answer == null || answer == "")){
            alert("请输入完整~");
            //location.href = 'add.html';
            return;
        }
        $.ajax({
            type: "POST",
            url: "problem/add",
            data: {
                "title": $("#title").val(),
                "level": $("#level").val(),
                "description": $("#desc").val(),
                "answer": $("#answer").val(),
                "templateCode": editor1.getValue(),
                "testCode": editor2.getValue()
            },//把body转成json格式
            success: function(data){
                window.location.href='index.html'
            }
        });
    }

    // function makeProblemDetail(problem){
    //     let submitButton = document.querySelector("#submitButton");
    //     submitButton.onclick = function() {
    //         if($("#title").val() == null){
    //             alert("请输入题目~");
    //         }
    //         $.ajax({
    //             type: "POST",
    //             url: "problem/add",
    //             data: {
    //                 "title": $("#title").val(),
    //                 "level": $("#level").val(),
    //                 "description": $("#desc").val(),
    //                 "answer": $("#answer").val(),
    //                 "templateCode": editor1.getValue(),
    //                 "testCode": editor2.getValue()
    //             },//把body转成json格式
    //             success: function(data){
    //                 window.location.href='index.html'
    //             }
    //         });
    //     }
    // }


</script>
</body>
</html>
